/**
 * 
 */
package com.radeonsys.jsfmock.util;

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Represents a general purpose key-value pair implementation which can be used
 * to represent entries in a {@link Map}
 * 
 * @param <K>	the type parameter representing the key of this key-value pair
 * @param <V>	the type parameter representing the value of this key-value pair
 * 
 * @see	java.util.Map#Entry
 * 
 * @author Ranganath Kini (ranganathkini at gmail dot com)
 * @version 1.0
 */
public class KeyValuePair<K, V> implements Entry<K, V>, Serializable {
	
	/**
	 * The generated serial version UID for this class
	 */
	private static final long serialVersionUID = -2927274436634835731L;
	
	private final K _key;
	private V _value;

	/**
	 * Creates a new instance of <code>KeyValuePair</code> with the specified
	 * key
	 * 
	 * @param key	the key to be used by this key-value pair
	 */
	public KeyValuePair(K key) {
		_key = key;
	}
	
	/**
	 * Creates a new instance of <code>KeyValuePair</code> with the specified
	 * key and value
	 * 
	 * @param key		the key to be used by this key-value pair
	 * @param value		the value to be associated with the key
	 */
	public KeyValuePair(K key, V value) {
		this(key);
		_value = value;
	}

	/* (non-Javadoc)
	 * @see java.util.Map.Entry#getKey()
	 */
	public K getKey() {
		return _key;
	}

	/* (non-Javadoc)
	 * @see java.util.Map.Entry#getValue()
	 */
	public V getValue() {
		return _value;
	}

	/* (non-Javadoc)
	 * @see java.util.Map.Entry#setValue(java.lang.Object)
	 */
	public V setValue(V value) {
		return _value;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((_key == null) ? 0 : _key.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		@SuppressWarnings("unchecked")
		KeyValuePair other = (KeyValuePair) obj;
		if (_key == null) {
			if (other._key != null)
				return false;
		} else if (!_key.equals(other._key))
			return false;
		return true;
	}
	
}
